<?php

namespace app\admin\controller;

use app\BaseController;
use app\Request;
use \app\admin\model\Tag as TagModel;

/**
 * 标签业务逻辑控制器
 */
class Tag extends BaseController
{
    /**
     * 标签管理业务逻辑控制器首页
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(Request  $request){

        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where['manager_id'] = $this->userInfo['id'];
        }
        $result = TagModel::order('reorder desc id desc')->where($where)->page($page, $limit)->select();
        //增加商户信息
        $result = \app\admin\model\Manager::getMananger($result);
        $total = TagModel::where($where)->count('id');

        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    /**
     * @param Request $request
     */
    public function create(Request  $request){
        $data = input();
        $data['manager_id']  = $this->userInfo['id']??0;
        $result = TagModel::create($data);
        if($result){
            return show(config('status.success'), '操作成功');
        }else{
            return show(config('status.error'), '操作失败');
        }

    }
    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json409eff
     */
    public function update(Request $request)
    {
        $data = $request->post();
        $result = TagModel::where(['id' => $request->post('id')])->update($data);
        if($result){
            return show(config('status.success'), '操作成功qqqq');
        }else{
            return show(config('status.error'), '操作失败');
        }

    }

    /**
     * 删除标签业务逻辑控制器
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request){
        $result = TagModel::destroy($request->post('id'));
        if($result){
            return show(config('status.success'), '操作成功');
        }else{
            return show(config('status.error'), '操作成功');
        }

    }

    /**
     * 获取当前用户门店
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getStore(Request $request){
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where['manager_id'] = $this->userInfo['id'];
        }
        $result = \app\admin\model\Store::where($where)->select();
        $data['data'] = $result;
        return show(config('status.success'), '操作成功', $data);
    }
}